package com.cn;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Base64;
import java.util.Optional;


public class CodeApplication extends Application {

    public static void main(String[] args) {
//        launch(args);
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Pane pane = new Pane();


        Label labelKey = new Label();
        labelKey.setText("key:");
        labelKey.setLayoutX(20.0D);
        labelKey.setLayoutY(25.0D);
//        Label labelValue = new Label();
//        labelValue.setText("加密的值");
//        labelValue.setLayoutX(20.0D);
//        labelValue.setLayoutY(65.0D);
        TextField key = new TextField();
        key.setLayoutX(110.0D);
        key.setLayoutY(20.0D);
//        TextField value = new TextField();
//        value.setLayoutX(110.0D);
//        value.setLayoutY(60.0D);

        Label encryValue = new Label();
        encryValue.setText("选择加密方式");
        encryValue.setLayoutX(20.0D);
        encryValue.setLayoutY(100.0D);
        // 下拉选择
        ComboBox<String> comboBox = new ComboBox<>();
        comboBox.getItems().addAll("SM2", "RSA", "HASH");
        comboBox.setLayoutX(110.0D);
        comboBox.setLayoutY(100.0D);
        comboBox.setValue("SM2");// 默认值
//        comboBox.setOnAction(e -> {
//            encryValue.setText(comboBox.getValue());
//        });

//        TextField encryvalue = new TextField();
//        encryvalue.setLayoutX(110.0D);
//        encryvalue.setLayoutY(100.0D);
//        Button encryptButton = new Button("解密");
        Button encryptButton = new Button("计算Hash");
//        encryptButton.setLayoutX(230.0D);
//        encryptButton.setLayoutY(160.0D);
        encryptButton.setLayoutX(110.0D);
        encryptButton.setLayoutY(60.0D);
        encryptButton.setOnAction(e -> {
//            if (key.getText() != null && !"".equals(key.getText()) && encryvalue.getText() != null && !"".equals(encryvalue.getText())) {
            if (key.getText() != null && !"".equals(key.getText())) {
                System.out.println("按钮值 = " + encryptButton.getText());
                System.out.println("下拉选择的值 = " + comboBox.getValue());
                String encrypt = "";
                try {
                    MessageDigest instance = MessageDigest.getInstance("SHA-256");
                    byte[] digest = instance.digest(key.getText().getBytes(StandardCharsets.UTF_8));
                    encrypt = Base64.getEncoder().encodeToString(digest);
                    System.out.println("Base64 Encoder SHA-256 Hash = " + encrypt);
                } catch (Exception ex) {
                    alterShow("计算Hash失败");
                }
                if (encrypt != null && !"".equals(encrypt)) {
                    showPopupWithCopyButton(encrypt);
                }
            } else {
                alterShow("Key不能为空!");
            }
        });
        pane.getChildren().addAll(new Node[] { labelKey, key, encryValue, comboBox });
//        Button button = new Button("加密");
//        button.setLayoutX(150.0D);
//        button.setLayoutY(160.0D);
//        button.setOnAction(e -> {
//            if (key.getText() != null && !"".equals(key.getText()) && value.getText() != null && !"".equals(value.getText())) {
//                BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
//                textEncryptor.setPassword(key.getText());
//                showPopupWithCopyButton("ENC(" + textEncryptor.encrypt(value.getText()) + ")");
//            } else {
//                alterShow("Key和value不能为空!");
//            }
//        });

        Platform.setImplicitExit(false);
        stage.setOnCloseRequest(event -> {
            event.consume();
            Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
            Stage stage1 = (Stage) alert.getDialogPane().getScene().getWindow();
            stage1.getIcons().add(new Image("/image/1.png"));

            alert.setTitle("退出程序");
            alert.setHeaderText(null);
            alert.setContentText("你是否退出程序");
            Optional<ButtonType> result = alert.showAndWait();
            if (result.get() == ButtonType.OK) {
                Platform.exit();
            }
        });
        pane.getChildren().addAll(new Node[] { encryptButton });
        Scene scene = new Scene(pane, 350.0D, 200.0D);
        stage.setScene(scene);
        stage.getIcons().add(new Image("/image/1.png"));
        stage.setTitle("Hash计算APP");
        stage.setResizable(false);
        stage.show();
    }




    private void showPopupWithCopyButton(String value) {
        Alert alert = new Alert(Alert.AlertType.NONE);
        Stage stage1 = (Stage) alert.getDialogPane().getScene().getWindow();
        stage1.getIcons().add(new Image("/image/1.png"));

        alert.setTitle("Hash值");
        alert.setHeaderText("点击复制");

        alert.setContentText(value);

        ButtonType copyButtonType = new ButtonType("拷贝到剪贴板");

        alert.getButtonTypes().add(copyButtonType);


        alert.setResultConverter(button -> {
            System.out.println(button);
            if (button == copyButtonType) {

                String textToCopy = alert.getContentText();
                Clipboard clipboard = Clipboard.getSystemClipboard();
                ClipboardContent content = new ClipboardContent();
                content.putString(textToCopy);
                clipboard.setContent(content);
                if (clipboard.getString().equals(textToCopy)) {
                    Alert al = new Alert(Alert.AlertType.INFORMATION);
                    Stage stage2 = (Stage) al.getDialogPane().getScene().getWindow();
                    stage2.getIcons().add(new Image("/image/1.png"));

                    al.setTitle("信息");
                    al.setHeaderText(null);
                    al.setContentText("拷贝成功");

                    al.showAndWait();
                }
                return ButtonType.YES;
            }
            return null;
        });

        alert.showAndWait();
    }

    private void alterShow(String message) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        Stage stage1 = (Stage) alert.getDialogPane().getScene().getWindow();
        stage1.getIcons().add(new Image("/image/1.png"));

        alert.setTitle("错误");
        alert.setHeaderText("错误");
        alert.setContentText(message);
        alert.showAndWait();
    }
}
